<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/22
 * Time: 14:32
 */
class Controller
{
    static protected $outType = 'smarty'; // smarty json ajax
    public $tpl = null; // index.tpl
    public $out = null;

    /**
     * 页面根据$this->out和self::$outType输出内容
     */
    public function display()
    {
        // 如果out=true,会封装成['data'] = true输出
        if (is_bool($this->out)) {
            $_bool = $this->out;
            unset($this->out);
            $this->out['data'] = $_bool;
        }

        // 调试信息
        if (DEBUG && isset($GLOBALS['sys']['errorInfo'])) {
            if (self::$outType == 'string') {
                header('_debug:' . $GLOBALS['sys']['errorInfo']);
            } else if (is_array($this->out)) {
                $this->out['_debug'] = $GLOBALS['sys']['errorInfo'];
            }
        }

        // json跨域方式输出,
        // 如果有jsoncallback,默认用json输出
        if (self::$outType == 'json') {
            $callback = $this->R('jsoncallback');
            if (!preg_match('/^\w+/', $callback)) {
                $callback = '_';
            }
            if ($callback != '_') {
                header('Content-type: application/x-javascript');
            }
            exit($callback . '(' . json_encode(LibUtil::forceUtf8($this->out)) . ')');
        } // ajax直接输出
        elseif (self::$outType == 'ajax') {
            exit(json_encode($this->out));
        } elseif (self::$outType == 'string') {
            echo $this->out;
            exit();
        } // smarty输出
        // self::$outType == 'smarty'
        else {
            LibTemplate::displayAll($this->tpl, $this->out);
            if (!empty($this->out['_debug'])) {
                LibTemplate::assign("debug_info", $this->out['_debug']);
                LibTemplate::display(Chat_ROOT . '/template/debug.tpl');
            }
        }
    }

    /**
     * 设置输出类型及模板,如果是'ajax'或者'json',则以ajax,json输出.
     * 如果$outType = '*.tpl',则用smarty模板输出
     * @param $outType
     * @return bool
     * @throws LogicException
     */
    public function setOutType($outType)
    {
        if (!$outType) {
            throw new LogicException('$outType不得为空');
        }
        if ($outType == 'json' || $outType == 'ajax' || $outType == 'smarty' || $outType == 'string') {
            global $defaultOutType;
            self::$outType = $defaultOutType = $outType;

            return true;
        }
        throw new LogicException('$outType参数错误');
    }

    //获得Request的值
    public function R($key, $defaultValue = null, $varType = '', $trim=true, $filter=true){
        if (!$key) {
           return "";
        }
        if(!isset($_REQUEST[$key]) || $_REQUEST[$key] === false) {
            return $defaultValue;
        }else{
            return self::getValue($_REQUEST[$key], $varType, $trim, $filter);
        }
    }

    //格式化值
    public  function getValue($value, $varType, $trim, $filter){
        if(is_array($value)){
            foreach($value as &$v){
                $v = self::getValue($v, $varType, $trim, $filter);
            }
        }else{
            if (!empty($varType))
                settype($value, $varType);

            if($trim)
                $value = trim($value);

            if($filter)
                $value = htmlspecialchars($value);
        }
        return $value;
    }
}